// pages/index/weixiuHome/jiechedan/jiechedan.js
const request = require('../../../../utils/request')
const utils = require('../../../../utils/util')

Page({
  data: {
    type: '',
    page: 1,
    total: 0,
    repairList: []
  },

  onLoad(options) {
    this.setData({
      type: options.type
    })
  },

  onReady() {
    switch (this.data.type) {
      case 'jiechedan':
        this.setNavigationBarTitle('维修单')
        this.getRepairList(null)
        break
      case 'guazhangdan':
        this.setNavigationBarTitle('挂帐单')
        this.getRepairList(0)
        break
      default: 
        this.setNavigationBarTitle('结算单')
        this.getRepairList(1)
        break
    }
  },
  refreshData() {
    this.onReady()
  },
  setNavigationBarTitle(title) {
    wx.setNavigationBarTitle({ title })
  },

  getRepairList(type) {
    wx.showLoading()
    var url;
    var data;
    if (type == 0) {
      data = {
        status4: 1,
        page: this.data.page
      }
      url = 'repair_gj'
    } else {
      data = {
        status: type,
        page: this.data.page
      }
      url = 'repair_list'
    }
    request.get(url, data)
      .then(res => {
        wx.hideLoading()
        if (res.data && res.data.length) {
          this.setData({
            repairList: [...this.data.repairList, ...res.data],
            page: this.data.page + 1,
            total: res.pagenum
          })
        }
      })
      .catch(error => {
        wx.showToast({
          icon: 'none',
          title: error.msg && error.msg
        })
      })
      .finally(_ => {
        
      })
  },

  toDetail(e) {
    const id = e.currentTarget.dataset.id
    const isStaff = utils.isStaff()
    if (this.data.type != 'jiesuandan' && isStaff) {
      wx.navigateTo({
        url: `/pages/userCenter/jiedan/jiedan?id=${id}&type=${this.data.type}`
      })
    } else {
      wx.navigateTo({
        url: `/pages/index/weixiuHome/jiechedan/jiecheDetail/jiecheDetail?id=${id}&type=${this.data.type}`
      })
    }
  },

  onReachBottom: utils.throttle(function() {
    if (this.data.page <= this.data.total) {
      this.getList(this.data.type)
    } else {
      wx.showToast({
        icon: 'none',
        title: '已加载所有'
      })
    }
  }, 1000),

  onPullDownRefresh: utils.throttle(function () {
    this.setData({
      page: 1,
      repairList: []
    })
    this.getList(this.data.type)
  }, 1000),  

  getList(type) {
    switch (type) {
      case 'jiechedan':
        this.getRepairList(null)
        break
      case 'guazhangdan':
        this.getRepairList(0)
        break
      default: 
        this.getRepairList(1)
        break
    }
  },
  formSubmit(e) {
    var value = e.detail.value
    // if (value.chepai == '') {
    //   wx.showToast({
    //     icon: 'none',
    //     title: '请填写车牌'
    //   })
    //   return
    // }
    wx.showLoading()
    var url;
    var data;
    if (this.data.type == 'guazhangdan') {
      data = {
        status4: 1,
        chepai: value.chepai
      }
      url = 'repair_gj'
    } else {
      data = {
        status: 1,
        chepai: value.chepai
      }
      url = 'repair_list'
    }
    request.get(url, data)
      .then(res => {
        wx.hideLoading()

        if (res.data && res.data.length) {
          this.setData({
            repairList: res.data,
            page: 1,
            total: res.pagenum
          })
        } else {
          wx.showToast({
            icon: 'none',
            title: '没有找到相关记录'
          })
        }
      })
      .catch(error => {
        wx.showToast({
          icon: 'none',
          title: error.msg && error.msg
        })
      })
      .finally(_ => {

      })
  }
})
